/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia.entidade;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author Novo usuário
 */
@Entity
@Table(name = "categoria", schema = "")
public class Categoria implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "CATEGORIA_ID", nullable = false)
    private Integer categoriaId;
    @Basic(optional = false)
    @Column(name = "NOME", nullable = false, length = 250)
    private String nome;
    @Column(name = "RESUMO_FILHOS", length = 50)
    private String resumoFilhos;
    @JoinTable(name = "categoria_registro", joinColumns = {@JoinColumn(name = "CATEGORIA_ID", referencedColumnName = "CATEGORIA_ID", nullable = false)}, inverseJoinColumns = {@JoinColumn(name = "REGISTRO_ID", referencedColumnName = "REGISTRO_ID", nullable = false)})
    @ManyToMany(fetch = FetchType.EAGER)
    private List<Registro> registroCollection;
    @OneToMany(mappedBy = "categoriaPai", fetch = FetchType.EAGER)
    private List<Categoria> categoriaCollection;
    @JoinColumn(name = "CATEGORIA_PAI", referencedColumnName = "CATEGORIA_ID")
    @ManyToOne(fetch = FetchType.EAGER)
    private Categoria categoriaPai;
    @Lob
    @Column(name = "img_web", columnDefinition="longblob")
    private byte[] imgWeb;
    @Lob
    @Column(name = "img_mob", columnDefinition="longblob")
    private byte[] imgMob;

    public Categoria() {
    }

    public Categoria(Integer categoriaId) {
        this.categoriaId = categoriaId;
    }

    public Categoria(Integer categoriaId, String nome) {
        this.categoriaId = categoriaId;
        this.nome = nome;
    }

    public Integer getCategoriaId() {
        return categoriaId;
    }

    public void setCategoriaId(Integer categoriaId) {
        this.categoriaId = categoriaId;
    }

    public String getNome() {
        return nome;
    }

    public String getNomeAndCountRegs() {
        String name = nome;
        
        if(registroCollection == null)
            return nome;
        if(registroCollection.size() >= 1){
            name += " (" + getRegistroCollection().size() + " )";
        }
    /*    else if(categoriaCollection != null && categoriaCollection.size() == 0){
            name += " (0)";
        }*/
        return name;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getResumoFilhos() {
        return resumoFilhos;
    }

    public void setResumoFilhos(String descricao) {
        this.resumoFilhos = descricao;
    }

    public List<Registro> getRegistroCollection() {
        return registroCollection;
    }

    public void setRegistroCollection(List<Registro> registroCollection) {
        this.registroCollection = registroCollection;
    }

    public List<Categoria> getCategoriaCollection() {
        if(categoriaCollection == null){
            categoriaCollection = new ArrayList<Categoria>();
        }
        return categoriaCollection;
    }

    public void setCategoriaCollection(List<Categoria> categoriaCollection) {
        this.categoriaCollection = categoriaCollection;
    }

    public Categoria getCategoriaPai() {
        return categoriaPai;
    }

    public void setCategoriaPai(Categoria categoriaPai) {
        this.categoriaPai = categoriaPai;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (categoriaId != null ? categoriaId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Categoria)) {
            return false;
        }
        Categoria other = (Categoria) object;
        if ((this.categoriaId == null && other.categoriaId != null) || (this.categoriaId != null && !this.categoriaId.equals(other.categoriaId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {

        String str = getNome() + " (" + getRegistroCollection().size() + ")";
        Categoria pai = getCategoriaPai();
        if (pai != null) {
                str += " <- ";
        }
        while (pai != null) {
            str += pai.getNome();
            pai = pai.getCategoriaPai();
            if (pai != null) {
                str += " <- ";
            }
        }
        return str;

    }

    public String toMobile() {
        StringBuffer sb = new StringBuffer();

        sb.append(getCategoriaId());//0
        sb.append("|");
        sb.append(getNome());//1
        sb.append("|");
        String s = getResumoFilhos();
        if (s == null || "null".equals(s)) {
            s = "";
        }
        sb.append(s);//2
        sb.append("|");
        if (getCategoriaPai() != null) {
            sb.append(getCategoriaPai().getCategoriaId());
        }
        sb.append("|");
        List<Registro> regList = getRegistroCollection();
        sb.append(regList.size());//3
        sb.append("|");
        for (Iterator<Registro> regIt = regList.iterator(); regIt.hasNext();) {
            Registro reg = regIt.next();
            sb.append(reg.getRegistroId());
            sb.append("|");
        }

        List<Categoria> catList = getCategoriaCollection();
        sb.append(catList.size());//3
        sb.append("|");
        for (Iterator<Categoria> catIt = catList.iterator(); catIt.hasNext();) {
            Categoria cat = catIt.next();
            sb.append(cat.getCategoriaId());
            sb.append("|");
        }

        sb.delete(sb.length() - 1, sb.length());
        return sb.toString();
    }

    /**
     * @return the imgWeb
     */
    public byte[] getImgWeb() {
        return imgWeb;
    }

    /**
     * @param imgWeb the imgWeb to set
     */
    public void setImgWeb(byte[] imgWeb) {
        this.imgWeb = imgWeb;
    }

    /**
     * @return the imgMob
     */
    public byte[] getImgMob() {
        return imgMob;
    }

    /**
     * @param imgMob the imgMob to set
     */
    public void setImgMob(byte[] imgMob) {
        this.imgMob = imgMob;
    }
}
